<?php

/**
 * 后台密码加密
 * @param $password
 * @return string|bool
 */
if(!function_exists('password_encrypt')){
	function password_encrypt($password){
		return password_hash('llcms'.$password,PASSWORD_BCRYPT);
	}
}

/**
 * 后台密码验证
 * @param $password
 * @return string|bool
 */
if(!function_exists('password_verifymy')){
	function password_verifymy($password,$mypassword){
		return password_verify('llcms'.$password,$mypassword);
	}
}

/**
 * 树状结构
 * @param $array
 * @param $pid
 * @return array
 */
if(!function_exists('tree')){
	function tree($array,$pid=0)
	{
	    $tree = array();
	    foreach ($array as $key => $value) {
	        if ($value['pid'] == $pid) {
	            $value['child'] = tree($array, $value['id']);
	            if (!$value['child']) {
	                unset($value['child']);
	            }
	            $tree[] = $value;
	        }
	    }
	    return $tree;
	}
}

/**
 * 权限树状结构设置
 * @param $array
 * @param $pid
 * @return array
 */
if(!function_exists('roleAccessTree')){
	function roleAccessTree($array,$ids,$pid=0)
	{
	    $tree = array();
	    foreach ($array as $key => $value) {
	    	$value['title']=$value['name'];
	    	$value['spread']=true;
	        if ($value['pid'] == $pid) {
	            $value['children'] = roleAccessTree($array,$ids,$value['id']);
	            if(empty($value['children']) && in_array($value['id'],$ids)){
		    		$value['checked']=true;
		    	}
	            if (!$value['children']) {
	                unset($value['children']);
	                unset($value['child']);
	            }

	            $tree[] = $value;
	        }
	    }
	    return $tree;
	}
}
/**
 * 菜单树状结构
 */
if(!function_exists('roleMenuTree')){
	function roleMenuTree($array,$pid=0)
	{
	    $tree = array();
	    foreach ($array as $key => $value) {
	    	if($value['is_show']==1){
	    		$value['title']=$value['name'];
		    	$value['target']=$value['is_blank']==1?'_blank':'_self';
		    	$value['href']="";
		    	if(!empty($value['route'])){
		    		$value['href']=(string)url($value['route']);
		    	}
		        if ($value['pid'] == $pid) {
		            $value['child'] = roleMenuTree($array, $value['id']);
		            if (!$value['child']) {
		                unset($value['child']);
		            }
		            $tree[] = $value;
		        }
	    	}
	    }
	    return $tree;
	}
}
/**
 * 树状转一维数组
 */
if(!function_exists('treeToArray')){
	function treeToArray($array,$childKey="children"){
		$data=array();
		foreach ($array as $key => $value) {
			if(!empty($value[$childKey])){
				$childList=treeToArray($value[$childKey]);
				unset($value[$childKey]);
				$data=array_merge($childList,$data);
			}
			$data[]=$value;
		}
		return $data;
	}
}

if(!function_exists('selectAccess')){
	function selectAccess($array,$pid=0,$leftHtml="-| ",$leftnum=0){
		$Access=[];
		foreach ($array as $key => $value) {
			if($value['pid']==$pid){
				$Access[]=[
					'name'=>str_repeat($leftHtml,$leftnum).$value['name'],
					'id'  =>$value['id'],
				];
				$Access=array_merge($Access,selectAccess($array,$value['id'],$leftHtml,$leftnum+1));
			}
		}
		return $Access;
	}
}
